package com.linglan.common.api;

import com.linglan.common.constants.CommonConstants;
import com.linglan.common.utils.LingLanValidator;
import com.linglan.common.utils.I18nMessageUtil;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 */
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 返回标记：成功标记=0，失败标记=1
     */
    @Getter
    @Setter
    private int code;

    /**
     * 返回信息
     */
    @Getter
    @Setter
    private String msg;

    /**
     * 数据
     */
    @Getter
    @Setter
    private T data;
    /**
     * api 生成时间
     */
    @Getter
    @Setter
    private long timestamp = System.currentTimeMillis();

    @Getter
    @Setter
    private boolean isSuccess;

    private R(IResultCode resultCode) {
        this(resultCode, null, resultCode.getMessage());
    }

    private R(IResultCode resultCode, String msg) {
        this(resultCode, null, msg);
    }

    private R(IResultCode resultCode, T data) {
        this(resultCode, data, resultCode.getMessage());
    }

    private R(IResultCode resultCode, T data, String msg) {
        this(resultCode.getCode(), data, msg);
    }

    private R(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.isSuccess = ResultCode.SUCCESS.code == code;
    }

    public boolean isSuccess() {
        return getCode() == CommonConstants.SUCCESS ? true : false;
    }

    public boolean isNotSuccess() {
        return getCode() != CommonConstants.SUCCESS ? true : false;
    }


    public static <T> R<T> ok() {
        return restResult(null, CommonConstants.SUCCESS, null);
    }

    /**
     * 通过状态生成返回接口
     *
     * @param flag
     * @param <T>
     * @return
     */
    public static <T> R<T> status(boolean flag) {
        return flag ? ok() : failed();
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, CommonConstants.SUCCESS, null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, CommonConstants.FAIL, null);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, CommonConstants.FAIL, msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, CommonConstants.FAIL, null);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }
    public static <T> R<T> failed(T data, String msg, int code) {
        return restResult(data, code, msg);
    }
    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        if (LingLanValidator.isNotNullOrEmpty(msg)) {
            apiResult.setMsg(I18nMessageUtil.get(msg));
        }
        return apiResult;
    }

}
